﻿using AutoMapper;
using MDT.LifeSewagePlatform.Conmmon;
using MDT.LifeSewagePlatform.Entity;
using MDT.LifeSewagePlatform.Entity.View;
using MDT.LifeSewagePlatform.InterfaceService;
using MDT.LifeSewagePlatform.IRepository;
using MDT.LifeSewagePlatform.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MDT.LifeSewagePlatform.Service
{
    /**
    * 命名空间: MDT.LifeSewagePlatform.Service
    *
    * 功 能： N/A
    * 类 名： IpAddressService
    *
    * Ver 变更日期 负责人 变更内容
    * ───────────────────────────────────
    * V0.01 2022/2/14 15:41:28 
    *
    *┌──────────────────────────────────┐
    *│　***************************************************．　│
    *│　**********************　　　　　　　　　　　　　　│
    *└──────────────────────────────────┘
*/
    public class IpAddressService:BaseService,IIpAddressService
    {
        [Autowired]
        public IIpAddressRepository ipAddressRepository { get; set; }

        [Autowired]
        public IMapper mapper { get; set;}
        public IpAddressService()
        {

        }
        public async Task<BaseMessage> AddAsync(IpAddressDto ipAddressDto)
        {
            IpAddressEntity entity=mapper.Map<IpAddressEntity>(ipAddressDto);
            await ipAddressRepository.Create(entity,0);
            if (await ipAddressRepository.SaveChangesAsync())
            {
                return Ok200("添加成功");
            }
            return BadRequest400("添加失败!");
        }
         public async Task<BaseMessage> UpdateAsync(int Id,IpAddressDto ipAddressDto)
         {
            if (await ipAddressRepository.AnyAsync(ip=>ip.ID==Id)==false)
            {
                return BadRequest400("更新失败,该ip不存在!");
            }

            IpAddressEntity entity = await ipAddressRepository.FirstOrDefaultAsync(ip=>ip.ID==Id);
                mapper.Map(ipAddressDto,entity);
            await ipAddressRepository.Update(entity,0);
            if (await ipAddressRepository.SaveChangesAsync())
            {
                return Ok200("更新成功");
            }
            return BadRequest400("更新失败!");
        }

         public async Task<BaseMessage> DeleteAsync(int Id)
         {
            if (await ipAddressRepository.AnyAsync(ip => ip.ID ==Id)==false)
            {
                return BadRequest400("删除失败,该ip不存在!");
            }
            await ipAddressRepository.Delete(ip => ip.ID == Id);
            if (await ipAddressRepository.SaveChangesAsync())
            {
                return Ok200("删除成功!");
            }
            return BadRequest400("删除失败!");

        }
        public async Task<BaseMessage> SoftDeleteAsync(int Id,int UserId)
        {
            if (await ipAddressRepository.AnyAsync(ip => ip.ID == Id)==false)
            {
                return BadRequest400("删除失败,该ip不存在!");
            }
            await ipAddressRepository.SoftDelete(ip => ip.ID == Id,UserId);
            if (await ipAddressRepository.SaveChangesAsync())
            {
                return Ok200("删除成功!");
            }
            return BadRequest400("删除失败!");
        }
        public async Task<BaseMessage> GetIpAdressAsync(IpAddressParameter Params)
        {
          var query= ipAddressRepository.GetIpAddresses(ip=>Params.IsBad==ip.IsBad);

            if (Params.IpAddress>0)
            {
                query = query.Where(ip=>ip.IpAddress==Params.IpAddress);
            }

            if (Params.Id > 0)
            {
                query = query.Where(ip => ip.Id == Params.Id);
            }
            return Ok200("查询成功!",await PageList<IpAddressView>.CreateAsync(query,Params));
        }
    }
}
